// 钱包余额提现页面
var basePage = require('../../../common/BasePage');
Page({
    behaviors: [basePage],
    data: {
        /**支付凭证 */
        pay_cert: {
            /**完整图片路径 */
            url: '/assets/photo.png',
            /**后台地址 */
            photo: '',
            /**''未选择;loading 上传中;error 上传失败;ok 上传成功 */
            state: '',
        },
        /**密码设置弹窗 */
        editPassPop: false,
        /**重新设置的密码 */
        repass: '',
        /**支付参数 */
        payData: {code:'wxpay'},
        /**支付密码 */
        password: '',
        /**是否显示密码输入框 */
        passPop: false,
        /**支付方式 */
        pay: 'wxpay',
        /**订单数据 */
        order: {},
        price: 0,
        /**是否可以提交 */
        canSubmit:true
    },
    onLoad: function (options) {
        let that = this;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('order', function (data) {
            console.log("orderData", data);
            that.setData({
                order: data
            })
            // that.setData({order:data});
            // that.request({
            //     url: this.data.host + '/home/order/detail?order_id=' + data.order_id ,
            // }, res => {
            //     console.log("order/detail",res)
            // })
        })
    },
    /**
     * 凭证上传
     */
    img_upload(e) {
        let that = this;
        /**上传图片的对象 */
        let img = e.currentTarget.dataset.img;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(imgRes) {
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = imgRes.tempFilePaths;
                console.log("tempFilePaths", tempFilePaths);
                let photo = {}; //用于更新视图诗句
                let id_photo_front = photo[img] = that.data[img];
                id_photo_front.url = tempFilePaths[0];
                id_photo_front.state = "loading";
                console.log("photo", photo)
                that.setData(photo)
                wx.uploadFile({
                    filePath: tempFilePaths[0],
                    name: 'image',
                    url: that.data.host + '/home/upload/image',
                    formData: {
                        from: 'verify'
                    },
                    success(res) {
                        id_photo_front.state = "error";
                        // id_photo_front.photo='';
                        // console.log("home/upload/image", res);
                        if (res.statusCode != 200) {
                            return wx.showModal({
                                showCancel: false,
                                content: res.errMsg || '上传失败',
                            })
                        }
                        let resObj = JSON.parse(res.data);
                        if (resObj.code) {
                            return wx.showToast({
                                title: resObj.msg,
                                icon: 'none'
                            })
                        }
                        id_photo_front.state = "ok";
                        id_photo_front.photo = resObj.data.path;

                    },
                    fail() {
                        id_photo_front.state = "error";
                        wx.showModal({
                            showCancel: false,
                            content: '上传接口异常',
                        })
                    },
                    complete() {
                        that.setData(photo)
                    }
                });
            },
            fail(e) {
                wx.showToast({
                    title: e.errMsg || '调用微信文件失败',
                    icon: 'none'
                })
                // console.log(e);
            }
        })
    },
    /**设置支付密码 */
    edit_pass() {
        this.request({
            url: this.data.host + '/home/user/editPasswd',
            data: {
                password: this.data.repass
            },
            method: 'post'
        }, res => {
            wx.showToast({
                title: '设置成功',
                icon: 'none'
            });
            this.setData({
                editPassPop: false
            })
        });
    },
    /**检查支付方式 */
    check_pay() {
        // if (this.data.pay == "money") { //密码支付，弹出密码输入框
        //     return this.setData({
        //         passPop: true
        //     })
        // }
        this.pay_order()
    },
    submit_pass() {
        if (!this.data.password) {
            return wx.showToast({
                title: '请先输入密码',
                icon: 'none'
            })
        }
        this.setData({
            passPop: false
        })
        this.data.payData.password = this.data.password;
        this.pay_order()
        // console.log("password",this.data.password);
        // console.log("payData",this.data.payData);
    },
    /**
     * 支付订单
     */
    pay_order() {
        if(!this.data.canSubmit){
            return wx.showToast({
              title: '请勿频繁提交',
              icon:'none'
            })
        }
        this.data.canSubmit=false;
        setTimeout(() => {
            this.data.canSubmit=true;
        }, 1000);
        // let that=this;
        this.data.pay_cert.photo ? this.data.payData.pay_cert = this.data.pay_cert.photo : '';
        this.data.payData.order_id=this.data.order.order_id
        this.request({
            url: this.data.host + '/home/payment/order',
            method: "POST",
            data: this.data.payData
        }, res => {
            //  console.log("payment/order",res,this.data.payData.code);
            if (this.data.payData.code == 'wxpay') { //调用微信支付
                wx.requestPayment({
                    ...res.data.pay_data,
                    success(res) {
                        wx.showToast({
                            title: '支付成功',
                        })

                    },
                    fail(err) {
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none'
                        })
                        console.error('pay fail', err)
                    },
                    complete() {
                        wx.redirectTo({
                            url: '/pages/cls/my_order/order',
                        })
                    }
                })
            } else {
                wx.redirectTo({
                    url: '/pages/cls/my_order/order',
                })
            }
            //  return;

        });
    },
    onClick(event) {
        let name = event.currentTarget.dataset.name;
        this.data.payData.code = name;
        this.setData({
            pay: name,
        })
    },
    /**
     * 确认订单（立即支付）
     */
    submit_order() {
        this.check_pay();
        // if (this.data.payData && this.data.payData.order_id) {
        //     return this.check_pay(); //已经下单成功，直接进行支付
        // }
        // //请求购物车数据
        // this.request({
        //     url: this.data.host + '/home/cart/handle?wh_id=1',
        //     data: {
        //         addr_id: this.data.m_addr.addr_id,
        //         cid: this.data.coupon.cid
        //     },
        //     method: 'post'
        // }, this.submit_ok);
    },
    /**显示密码重置弹窗 */
    show_edit() {
        this.setData({
            editPassPop: true
        })
    }
})